<?
	foreach(explode('/',  $url[0]) as $value)
	{
		if (!empty($value))
			$parts[] = trim($value);
	}

	foreach($routes as $route)
	{
		$data = array();
		$values  = array();
		$rParts = explode('/', $route['path']);

		foreach($parts as $key=>$val)
		{
			if(empty($rParts[$key]))
			{
				$values[] = $val;
			}
			else
			{
				$name = $rParts[$key];

				$data[$name] = $route['values'][$name];

				if(isset($val))
				{
					$data[$name] = $val;
				}
			}
		}

		foreach($route['values'] as $key=>$val)
		{
			if(empty($data[$key]))
			{
				$data[$key] = $val;
			}
		}

        $hasFile = false;

        foreach($dirs as $dir)
        {
            $tmp = $dir . $data['controller'] . '_controller.php';

            if(is_file($tmp))
            {
                $hasFile = true;
                require_once($tmp);
            }
        }

        if($hasFile)
        {
            $controllerName = $data['controller'] . 'Controller';

            $controller = new $controllerName($data, $values);
            $actionName = $data['action'];
            $reflection = new ReflectionClass($controllerName);

            if($reflection->hasMethod($actionName))
            {
                $action = $reflection->getMethod($actionName);
                $argsInfo = $action->getParameters();
                $argsMax = count($argsInfo);
				$allowNull = false;
				
				
				foreach($argsInfo as $info)
				{
					if($info->allowsNull() || $info->isDefaultValueAvailable())
					{
						$allowNull = true;
						break;
					}
				}
				
                if($allowNull || $argsMax == count($values) || $argsMax == 0)
                {
                    $controller->action_begin($actionName);
                    $action->InvokeArgs($controller, $values);
                    $controller->action_end($actionName);
                }

                break;
            }
        }
	}
?>